﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaBastanteo.Negocio;
using SistemaBastanteo.Dominio;

namespace SistemaBastanteo.Controllers
{
    public class TipoDocumentoController : Controller
    {
        //
        // GET: /TipoDocumento/
        public IMantenimientoService AdminService { get; set; }

        public ActionResult Index()
        {
            ICollection<TipoDocumento> modelo = AdminService.ListarTipoDocumento();
            return View(modelo);
        }

        //
        // GET: /TipoDocumento/Details/5

        public ActionResult Details(int id)
        {
            TipoDocumento modelo = AdminService.ObtenerTipoDocumento(id);
            return View(modelo);
        }

        //
        // GET: /TipoDocumento/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /TipoDocumento/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // TODO: Add insert logic here
                    string descripcion = (string)collection["Descripcion"];



                    if (AdminService.ObtenerTipoDocumentoPorDescripcion(descripcion).Count > 0)
                        throw new Exception("El TipoDocumento ingresado ya existe");

                    AdminService.RegistrarTipoDocumento(descripcion);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }

            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
                return View();
            }
        }
        
        //
        // GET: /TipoDocumento/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /TipoDocumento/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TipoDocumento/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TipoDocumento/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
